
package com.googlecode.mgwt.examples.showcase.client.activities.animation;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.googlecode.mgwt.dom.client.event.tap.TapEvent;
import com.googlecode.mgwt.dom.client.event.tap.TapHandler;
import com.googlecode.mgwt.examples.showcase.client.AppController;
import com.googlecode.mgwt.examples.showcase.client.ClientFactory;
import com.googlecode.mgwt.examples.showcase.client.activities.animation.Animation.AnimationNames;
import com.googlecode.mgwt.examples.showcase.client.event.ActionEvent;
import com.googlecode.mgwt.examples.showcase.client.event.ActionNames;
import com.googlecode.mgwt.examples.showcase.client.places.HomePlace;
import com.googlecode.mgwt.examples.showcase.client.rpc.AppAsyncCallback;
import com.googlecode.mgwt.examples.showcase.shared.ServiceCallDTO;
import com.googlecode.mgwt.examples.showcase.shared.exception.ElementExistsException;
import com.googlecode.mgwt.mvp.client.MGWTAbstractActivity;
import com.googlecode.mgwt.mvp.client.history.HistoryHandler;
import com.googlecode.mgwt.ui.client.dialog.Dialogs.ButtonType;
import com.googlecode.mgwt.ui.client.dialog.Dialogs.OptionCallback;
import com.googlecode.mgwt.ui.client.dialog.Dialogs.OptionsDialogEntry;


public class ServiceRequestActivity extends MGWTAbstractActivity {

	private final ClientFactory clientFactory;
	private TapHandler postTapHandler;

	/**
	 * 
	 */
	public ServiceRequestActivity(ClientFactory clientFactory) {
		this.clientFactory = clientFactory;

	}
	
	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		ServiceRequesView view = clientFactory.getServiceRequestView();
		view.setLeftButtonText(clientFactory.getUserProfile().getCurrentUser().getLabels().main());
		view.setTitle(AppController.getClientFactory().getUserProfile().getCurrentUser().getLabels().reportForm());
		attachHandlers(eventBus);
		panel.setWidget(view);
		
	}


	private void attachHandlers(final EventBus aEventBus) {
		final ServiceRequesView view = clientFactory.getServiceRequestView();
			postTapHandler = getPostTapHandler(aEventBus, view);
			addHandlerRegistration(view.getPostButton().addTapHandler(postTapHandler));
			addHandlerRegistration(view.getBackButton().addTapHandler(new TapHandler() {

				@Override
				public void onTap(TapEvent event) {
					clientFactory.getPlaceController().goTo(new HomePlace(null));
//					ActionEvent.fire(aEventBus, ActionNames.BACK);

				}
			}));

	}

	private TapHandler getPostTapHandler(final EventBus aEventBus,final ServiceRequesView view) {
			return new TapHandler() {
				@Override
				public void onTap(TapEvent event) {
					
					if(! view.validate()){
						return;
					}
					List<OptionsDialogEntry> list = new ArrayList<OptionsDialogEntry>();
					list.add(new OptionsDialogEntry(AppController.getClientFactory().getUserProfile().getCurrentUser().getLabels().ok(), ButtonType.CONFIRM));
					list.add(new OptionsDialogEntry(AppController.getClientFactory().getUserProfile().getCurrentUser().getLabels().cancel(), ButtonType.NORMAL));
					view.confirm(list, new OptionCallback() {

						@Override
						public void onOptionSelected(int index) {
							if(index == 2){
								return;
							}
							view.setBussy(true);
							ServiceCallDTO dto = view.getServiceCallDTO();
							dto.setBuildingID(clientFactory.getUserProfile().getCurrentUser().ordinal());
							AppController.getClientFactory().getService().createServiceCall(dto, new AppAsyncCallback<Void>() {

								@Override
								public void onInnerSuccess(Void aResult) {
									view.setBussy(false);
									AnimationSelectedEvent.fire(aEventBus, new Animation(AnimationNames.FLIP, "Flip",null));
								}
								@Override
								public void onFailure(Throwable caught) {
									view.setBussy(false);
									if(caught instanceof ElementExistsException){
										ServiceCallDTO dto = ((ElementExistsException)caught).getDto();
										view.showDuplicateMsg(dto);
									}
								}
							});
						}
					});
					



				}
			};
	}

	
	



}
